<?php

namespace app\pay\controller;

use think\Controller;
use think\Db;

/**
 * 队列实际执行逻辑
 * Class Task
 * @package app\pay\controller
 */
class Task extends Base
{
    /**
     * 手动测试代偿逻辑方法
     */
	public function index($id = "", $kk = "")
	{
		//测试的时候从新更新队列
		if ($kk) {
			Db::name('mission')->where('mission_id', $id)->update(['mission_queues' => 0]);
		}


		//测试的时候传入的id
		if ($id) {
			$where['mission_id'] = $id;
		}else{
			$where['mission_state'] = 1;//0未启动1进行中
			$where['mission_type'] = 0;//0不需要补单 1需要补单 2 失败 3 支付中
			//$where['mission_queues'] = 0;//0未加入列队 1提交列队中 2提交列队失败 3列队处理成功 4列队处理失败
		}

		$where["mission_del"] = 0;

		$missionlist = Db::name('mission')
			->where($where)
			->field("mission_id,mission_uid,mission_cid,mission_pay_id,mission_pay_time,mission_current_state,mission_region_id")
			->order('mission_pay_time ASC')
			//->fetchSql(1)
			->select();
		//halt($missionlist);
		if (empty($missionlist)) {
			echo "没有可以执行的计划" . PHP_EOL;exit();
		}
		unset($where);
		foreach ($missionlist as $key => $v) {
			//先取出一个账单
			$plan = Db::name('plan')
				->where(['plan_mid' => $v['mission_id'], 'plan_state' => 0])
				->order('plan_pay_time ASC')
				->find();
			if (empty($plan)) {
				echo '计划不存在 [' . $v['mission_id'] . ']<br/>';
				break;
			}

			$card = Db::name('user_card')->where('card_id', $v['mission_cid'])->where('card_type', 1)->find();
			if (empty($card)) {
				echo '计划 [' . $v['mission_id'] . '] 银行卡不存在<br/>';
				break;
			}

			$payment = Db::name('payment')
				->where('payment_id', $v['mission_pay_id'])
				->field("payment_id,payment_controller")
				->find();
			if (empty($payment)) {
				echo '计划 [' . $v['mission_id'] . '] 通道不存在<br/>';
				break;
			}
			$res = controller("pay/".$payment["payment_controller"])->pay($v);
			dump($res);
		}
	}


	/**
	 * 询待处理订单
	 */
    public function processed($plan_form_no="")
    {
    	$where=[];
        if ($plan_form_no) {
            $where['plan_form_no'] = $plan_form_no;
        }
		$where['plan_state'] = 3;
        $plan = Db::name('plan')->where($where)->order('plan_time ASC')->select();
        if (empty($plan)) {
            echo "没有待处理订单";exit();
        }
        foreach ($plan as $k => $v) {
            $payment = Db::name('payment')->where('payment_id', $v['plan_pay_id'])->find();
            if (empty($payment)) {
                echo 'plan_id [' . $v['plan_id'] . '] 通道不存在<br/>';
                break;
            }
			$result = Controller("pay/".$payment['payment_controller'])->query_order_status($v);
			dump($result);
        }
    }


    public function autoHk(){
		$list = Db::name("repayment_fail_log")->where(["status"=>0])->select();
		foreach ($list as $item) {
			return controller("pay/".$item["payment_controller"])->autoHuan($item);
		}
	}


	public function autoCashQueryOrder(){
		$list = Db::name("user_profit")->where(["profit_type"=>4])->select();
		if(empty($list)){
			return "暂无订单";
		}
		foreach ($list as $item){
			(new Payhqdf())->queryOrder($item);
		}
	}
    /**
     * 查询快捷收款的待处理订单
     * @Author tw
     * @Date   2018-10-23
     * @return [type]     [description]
     */
    public function processed_sk($id=0)
    {
		$where=[];
		if ($id) {
			$where['records_id'] = $id;
		}


        $order = Db::name('pay_records')
            ->where($where)
            ->whereIn('records_state', '3,4,5')
            ->order('records_pay_time ASC')
            ->select();
        if (empty($order)) {
            echo "没有待处理订单";
            exit();
        }

        foreach ($order as $k => $v) {
            $payment = Db::name('payment')->where('payment_id', $v['records_pay_id'])->find();
            if (empty($payment)) {
                $this->payment_kj($v['records_id'], $v['records_uid'], 2, '', '', '通道不存在', '', '');
                echo 'records_id [' . $v['records_id'] . '] 通道不存在-已终止订单<br/>';
                break;
            }
            $result = Controller("pay/".$payment['payment_controller'])->query_order_status($v);
            dump($result);
        }
    }



    //更新收款方法
	public function updateSkInfo($id){
		$info = Db::name('pay_records')
			->where($where)
			->whereIn('records_state', '3,4,5')
			->order('records_pay_time ASC')
			->select();
	}



	/**
	 * 还款补单
	 * @Author tw
	 * @return [type] [description]
	 */
	public function repayment_bd()
	{
		$plan_id = input('get.id');
		$form_no = input('get.form_no');
		if (empty($plan_id)) {
			return json(['error' => 1, 'msg' => '订单错误']);
		}

		$plan = Db::name('plan')->where('plan_id', $plan_id)->where('plan_form_no', $form_no)->where('plan_state', 2)->find();

		if (empty($plan)) {
			return json(['error' => 1, 'msg' => '该订单无需补单']);
		}
		$mission = Db::name('mission')->where('mission_id', $plan['plan_mid'])->find();
		$payment = Db::name('payment')->where('payment_id', $mission['mission_pay_id'])->find();

		$order_status = Controller($payment['payment_controller'])->query_order_status($plan);
		if ($order_status['error'] == 0) {
			return json($order_status);
		}

		if ($payment['payment_orders'] == '1') {
			if ($plan['plan_type'] == 1) {
				return json(['error' => 1, 'msg' => '还款不支持补单']);
			}
			$planlist = Db::name('plan')->where('plan_mid', $plan['plan_mid'])->whereIn('plan_state', [0, 2])->where('plan_sort', $plan['plan_sort'])->select();
			foreach ($planlist as $key => $value) {
				Db::name('plan')->where('plan_id', $value['plan_id'])->update(['plan_state' => 0, 'plan_form_no' => $value['plan_form_no'] . 'BD']);
			}
			$plan['plan_form_no'] = $value['plan_form_no'] . 'BD';
		} else {
			Db::name('plan')->where('plan_id', $plan_id)->update(['plan_state' => 0, 'plan_form_no' => $form_no . 'BD']);
		}
		Db::name('mission')->where('mission_id', $plan['plan_mid'])->update(['mission_type' => 0, 'mission_queues' => 0]);

		return json(['error' => 0, 'msg' => '成功,等待支付!']);
		$res = $this->pay($mission, $plan, '', $payment, '');
		if ($res['error'] == 0) {
			return json(['error' => 0, 'msg' => '成功']);
		}
		return json($res);
	}
}